<?php
namespace Badmins\Controller;
use Think\Controller;
class GoodsController extends Controller {
    public function index(){
    	$listrow = (int)I('pageSize');
    	if(empty($listrow)){
    		$listrow = 15;
    	}
    	$status = I('status');
    	if($status!=''){
    		$where['go.status'] = $status;
    	}
    	$name = I('name');
    	if(!empty($name)){
    		$map['go.name'] = array('like',"%{$name}%");
    		$map['go.two_name']  = array('like', "%{$name}%");
    		$map['go.search_words']  = array('like',"%{$name}%");
    		$map['_logic'] = 'or';
    		$where['_complex'] = $map;
    	}
    	$brandid = I('brandid/d');
    	if($brandid){
    		$where['go.brandid'] = $brandid;
    	}
    	$join = "left join ".C('DB_PREFIX')."brand as b on b.id = go.brandid  left join ".C('DB_PREFIX')."commend as cg on go.commend_id = cg.id  left join ".C('DB_PREFIX')."shop as s on s.id = go.sid";
    	$class_id = I('class_id/d');
    	if($class_id){
    		$join  .= " left join ".C('DB_PREFIX')."category_extend as ce on ce.goods_id = go.id ";
    		$where ['ce.category_id'] = $class_id;
    	}
    	$commendid = I('commend_id/d');
    	if($commendid)
    	{
    		//$join  .= " left join ".C('DB_PREFIX')."commend_goods as cg on go.id = cg.goods_id ";
    		$where['go.commend_id']= $commendid;
    	}
    	$p = I('pageCurrent/d')?I('pageCurrent/d'):'1';
    	$db = M('goods as go');
    	$where['go.is_del'] = 0;
    	$count = $db->join($join)->where($where)->count();
    	$firstrow = ($p-1)*$listrow;
    	$list = lists('goods as go',$join,$where,"go.*,cg.name as commend_name,s.shop_name","go.id desc",$firstrow,$listrow,2);
    	if(!empty($list)){
    		foreach ($list as $i=>$va){
    			$list[$i]['product_id'] = M('products')->where("goods_id='{$va['id']}'")->order("sell_price asc")->getField('id');
    		}
    	}
    	if($count>$listrow){
    		$show['total'] = $count;
    		$show['listrow'] = $listrow;
    		$this->assign('show',$show);
    	}
    	$this->assign('list',$list);
    	//品牌
    	$band['status'] = 0;
    	$brandlist = lists('brand','',$band,'id,name','sort desc', 0,0,2);
    	$this->assign('brandlist',$brandlist);
    	
    	//分类列表
    	$class['status'] = 0;
    	$class['is_del'] = 0;
    	$res = lists('category','',$class,"id,name,parent_id",'id desc', 0, 0,2);
    	$res = \Org\Oa\common::sortdata($res,0,'&nbsp;&nbsp;&nbsp;&nbsp;');
    	$this->assign('res',$res);
    	//推荐类型
    	$commend = lists('commend','',$band,'id,name','sort desc',0,0,2);
    	$this->assign('commend',$commend);
        $this->display();
    }
    public function nor() {
    	$goods_id = I('goods_id/d');//商品id
        $flag = I('id');
        $data['id'] = $flag;
        $data['is_del'] = 0;
        $spec_ids = M('model')->where($data)->getField('spec_ids');
        if(!empty($spec_ids)){
        	$map['id'] = array('in',$spec_ids);
        	$map['is_del'] = 0;
        	$order = "find_in_set(id,'".$spec_ids."')";
        	$speclist = M('spec')->where($map)->field("id,name,value,type")->order($order)->select();
        	foreach ($speclist as $i=>$va){
        		$value[$i] = explode(",",$va['value']);
        		foreach ($value[$i] as $k=>$vv){
        			$speclist[$i]['values'][$k]['value'] = $vv;
        			if($goods_id){
        				$is_spec[$k] = M('goods_spec')->where("goods_id='{$goods_id}' and spec_id='{$va['id']}' and spec_name='{$va['name']}' and spec_value='{$vv}'")->find();
        				if(!empty($is_spec[$k])){
        					$speclist[$i]['values'][$k]['ck'] = 1;
        				}else{
        					$speclist[$i]['values'][$k]['ck'] = 0;
        				} 
        			}
        		}
        	}
        	$this->assign('speclist', $speclist);
        }else{
        	$product = M('products')->where("goods_id='{$goods_id}'")->order("id desc")->find();
        	if(!empty($product)){
        		$group =  M('group_price')->where("goods_id='{$goods_id}' and product_id='{$product['id']}'")->select();
        		if(!empty($group)){
        			foreach ($group as $l=>$vl){
        				$grouplist[] = $vl['group_id']."_".$vl['price'];
        			}
        			$product['groupsting'] = join(",",$grouplist);
        		}
        	}
        	$this->assign('pro',$product);
        }
        $this->display();
    }
    public function tables() {
    	$spec_arr = I('spec_arr');
    	$goods_id = I('goods_id/d');
    	if(!empty($spec_arr)){
    		$td = "";
    		$tr = "";
    		$db = M('spec');
    		$list = R('Common/combineDika',array($spec_arr));//计算笛卡尔积
    		//print_r($list);exit();
    		$newres = array();
    		foreach ($spec_arr as $i=>$va){
    			$name[$i] = $db->where("id='{$i}'")->field("id,name,type")->find();
    			$td.="<td><b>".$name[$i]['name']."</b></td>";
    			//foreach ($va )
    			$name[$i]['value'] = join(",",$va);
    			$newres[] = array('id'=>$i,'spec_array'=>$name[$i]);
    		}
    		$json = jsonencode($newres);
    		$goodsinput = "<input type='hidden' name='goods_spec_array' value='".$json."'/>";
    		$count = count($spec_arr);
    		foreach ($list as $k=>$vaa){
    			$trtd[$k] = "";
    			for($j=1;$j<=$count;$j++){
    				$trtd[$k].="<td>".$vaa[$j-1]."</td>";
    				$spec_id[$k][] .= $vaa[$j-1];
    			}
    			$spec_id_string[$k] = join(",",$spec_id[$k]);
    			if($goods_id){
    			    $re[$k] = product_info($goods_id, $spec_id_string[$k]);
    			    if(!empty($re[$k])){
    			    	//组合会员组格式
    			    	$group[$k] =  M('group_price')->where("goods_id='{$goods_id}' and product_id='{$re[$k]['id']}'")->select();
    			    	if(!empty($group[$k])){
    			    		foreach ($group[$k] as $l=>$vl){
    			    			$grouplist[$k][] = $vl['group_id']."_".$vl['price'];	
    			    		}
    			    		$groupsting[$k] = join(",",$grouplist[$k]);
    			    	}
    			    	
    			    }
    			}
    			$inputhidden[$k] = '<input type="hidden" name="spec_array[]" value="'.$spec_id_string[$k].'"/>';
    			$goods_no[$k] = $re[$k]['products_no']?$re[$k]['products_no']:goods_no($k+1);
    			//是否有会员组价格
    			if(empty($groupsting[$k])){
    				$groupclass[$k] =  'class="btn btn-default groupprice"';
    			}else{
    				$groupclass[$k] =  'class="btn btn-red groupprice"';
    			}
    			//是否有规格图片
    			if(!empty($re[$k]['photo_list'])){
    				$photoclas[$k] = 'class="btn btn-red photolist"' ;
    			}else{
    				$photoclas[$k] = 'class="btn btn-green photolist"' ;
    			}
    			$tr.=' <tr data-value="'.$k.'" data-id="'.$re[$k]['id'].'">
    					<input type="hidden" class="" name="product_id[]" placeholder="" value="'.$re[$k]['id'].'"/>
		                <input type="hidden" class="gprice" name="group[]" placeholder="存放价格组的" value="'.$groupsting[$k].'"/>
    					<input type="hidden" class="gphoto" name="photo_list[]" placeholder="存放图片集合的" value="'.$re[$k]['photo_list'].'"/>
		                '.$inputhidden[$k].$trtd[$k].'
		                <td><input type="text" name="products_no[]" class="form-control" value="'.$goods_no[$k].'"/></td>
		                <td><input type="text" name="code[]" class="form-control" value="'.$re[$k]['code'].'"/></td>
		                <td><input text="text" name="market_price[]" class="form-control w60" value="'.$re[$k]['market_price'].'"/></td>
		                <td><input name="sell_price[]"  class="form-control w60"  value="'.$re[$k]['sell_price'].'"/> <button type="button" '.$groupclass[$k].' >价格组</button></td>
		                <td><input name="cost_price[]"  class="form-control w60" value="'.$re[$k]['cost_price'].'" /></td>
		                <td><input type="text" name="integral[]" class="form-control w60" value="'.$re[$k]['integral'].'"/></td>
		                <td><input type="text" name="weight[]" class="form-control w60" value="'.$re[$k]['weight'].'"/></td>
		                <td><input name="store_nums[]"  class="form-control w60" value="'.$re[$k]['store_nums'].'"/></td>
		                <td>
		                    <button type="button" class="btn btn-red del_spec">删</button>
		                    <button type="button" '.$photoclas[$k].'>图片</button>
		                </td>
		                </tr>';
    		}
    		
    	}
        $content = <<< EOT
        {$goodsinput}
        <table class='table table-bordered' id='spec_input_tab'>
            <tr>
                {$td}
                <td><b>商品货号</b></td>
                <td><b>说明</b></td>
                <td><b>市场价</b></td>
                <td><b>销售价|价格组</b></td>
                <td><b>成本价</b></td>
                <td><b>积分</b></td> 
                <td><b>重量</b></td>
                <td><b>库存</b></td>
                <td><b>操作</b></td>
            </tr>
            {$tr}
        </table>
EOT;
        echo $this->show($content);

    }

    public function tablestwo() {
        $vid = I('id');//模型id
        $goods_id = I('goods_id');//商品id
        $data['id'] = $vid;
        $data['is_del'] = 0;
        $spec_ids = M('model')->where($data)->getField('attr_ids');
        if(!empty($spec_ids)){
        	$map['id'] = array('in',$spec_ids);
        	$map['is_del'] = 0;
        	$order = "find_in_set(id,'".$spec_ids."')";
        	$attrlist = M('attribute')->where($map)->field("id,name,value,type")->order($order)->select();
        	if($goods_id){
        		foreach ($attrlist as $i=>$vaa){
        			$is_attr[$i] = M('goods_attribute')->where("goods_id='{$goods_id}' and attr_id='{$vaa['id']}' and attr_name='{$vaa['name']}'")->getField('attr_value');
        			$attrlist[$i]['true_value'] = $is_attr[$i];
        		}
        	}
        }
        if( !empty($attrlist)){
        	$tr = "";
        	foreach ($attrlist as $i=>$va){
        		$tr.= R('Common/html',array($va));
        	}
        $con = <<< EOT
    <table class='table table-bordered' id='attr_input_tab'>
        {$tr}
    </table>
EOT;
        }else{
            $con = '<div class="form-group"></div>';
        }
        echo $this->show($con);
    }
    /**
     * 商品分类
     */
    public function groy(){
    	$name = I('name');
    	if(!empty($name)){
    		$map['name'] = array('like',"{$name}");
    	}
    	$map['is_del'] = 0;
    	$list = lists('category','',$map,"*","id desc",0,0,2);
    	$list = \Org\Oa\common::sortdata($list,0);
    	$this->assign('list',$list);
    	$this->display();
    }
    /**
     * 模型列表
     */
    public function types(){
    	$listrow = (int)I('pageSize');
    	if(empty($listrow)){
    		$listrow = 15;
    	}
    	$p = I('pageCurrent/d')?I('pageCurrent/d'):'1';
    	$count = M('model')->count();
    	$firstrow = ($p-1)*$listrow;
    	$name = I('name');
    	if(!empty($name)){
    		$map['name'] = array('like',"{$name}");
    	}
    	$map['is_del'] = 0;
    	$list = lists('model','',$map,"id,name","id desc",$firstrow,$listrow,2);
    	if($count>$listrow){
    		$show['total'] = $count;
    		$show['listrow'] = $listrow;
    		$this->assign('show',$show);
    	}
    	$this->assign('list',$list);
    	$this->display();
    }
    /**
     * 规格列表
     */
    public function spec(){
    	$listrow = (int)I('pageSize');
    	if(empty($listrow)){
    		$listrow = 15;
    	}
    	$p = I('pageCurrent/d')?I('pageCurrent/d'):'1';
    	$count = M('spec')->count();
    	$firstrow = ($p-1)*$listrow;
    	$name = I('name');
    	if(!empty($name)){
    		$map['name'] = array('like',"{$name}");
    	}
    	$map['is_del'] = 0;
    	$list = lists('spec','',$map,"*","id desc",$firstrow,$listrow,2);
    	if($count>$listrow){
    		$show['total'] = $count;
    		$show['listrow'] = $listrow;
    		$this->assign('show',$show);
    	}
    	$this->assign('list',$list);
    	$this->display();
    }
    /**
     * 属性列表
     */
    public function attr(){
    	$listrow = (int)I('pageSize');
    	if(empty($listrow)){
    		$listrow = 15;
    	}
    	$p = I('pageCurrent/d')?I('pageCurrent/d'):'1';
    	$count = M('attribute')->count();
    	$firstrow = ($p-1)*$listrow;
    	$name = I('name');
    	if(!empty($name)){
    		$map['name'] = array('like',"{$name}");
    	}
    	$map['is_del'] = 0;
    	$list = lists('attribute','',$map,"*","id desc",$firstrow,$listrow,2);
    	if($count>$listrow){
    		$show['total'] = $count;
    		$show['listrow'] = $listrow;
    		$this->assign('show',$show);
    	}
    	$this->assign('list',$list);
    	$this->display();
    }
    /**
     * 品牌列表
     */
    public function brand(){
    	$listrow = (int)I('pageSize');
    	if(empty($listrow)){
    		$listrow = 15;
    	}
    	$p = I('pageCurrent/d')?I('pageCurrent/d'):'1';
    	$count = M('brand')->count();
    	$firstrow = ($p-1)*$listrow;
    	$map = 1;
    	$class_id = I('class_id/d');
    	if($class_id){
    		$map .= " and find_in_set('".$class_id."',category_ids)";
    	}
    	$name = I('name');
    	if(!empty($name)){
    		$map .= " and name like '%".$name."%'";
    	}
    	$list = lists('brand','',$map,"*","status asc,id desc",$firstrow,$listrow,2);
    	//echo M('brand')->getLastSql();
    	if(!empty($list)){
    		foreach ($list as $i=>$va){
    			if($va['category_ids']){
    				$data['id'] = array('in',$va['category_ids']);
    				$name[$i] = M('category')->where($data)->field("name")->select();
    				foreach ($name[$i] as $k=>$vv){
    					$arrayname[$i][] = $vv['name'];
    				}
    				$list[$i]['category_name'] = join(",",$arrayname[$i]);
    			}
    		}
    	}
    	if($count>$listrow){
    		$show['total'] = $count;
    		$show['listrow'] = $listrow;
    		$this->assign('show',$show);
    	}
    	$this->assign('list',$list);
    	//分类列表
    	$where['status'] = 0;
    	$where['is_del'] = 0;
    	$res = lists('category','',$where,"id,name,parent_id",'id desc', 0, 0,2);
    	$res = \Org\Oa\common::sortdata($res,0,'&nbsp;&nbsp;&nbsp;&nbsp;');
    	$this->assign('res',$res);
    	$this->display();
    }
    /**
     * 商品推荐类型
     */
    public function commend(){
    	$listrow = (int)I('pageSize');
    	if(empty($listrow)){
    		$listrow = 15;
    	}
    	$p = I('pageCurrent/d')?I('pageCurrent/d'):'1';
    	$count = M('commend')->count();
    	$firstrow = ($p-1)*$listrow;
    	$map = 1;
    	$name = I('name');
    	if(!empty($name)){
    		$map .= " and name like '%".$name."%'";
    	}
    	$list = lists('commend','',$map,"*","status asc,id desc",$firstrow,$listrow,2);
    	if($count>$listrow){
    		$show['total'] = $count;
    		$show['listrow'] = $listrow;
    		$this->assign('show',$show);
    	}
    	$this->assign('list',$list);
    	$this->display();
    }
    /**
     * 关键词
     */
    public function keyword_list(){
    	$listrow = (int)I('pageSize');
    	if(empty($listrow)){
    		$listrow = 15;
    	}
    	$p = I('pageCurrent/d')?I('pageCurrent/d'):'1';
    	$count = M('keyword')->count();
    	$firstrow = ($p-1)*$listrow;
    	$map = 1;
    	$name = I('name');
    	if(!empty($name)){
    		$map .= " and name like '%".$name."%'";
    	}
    	$list = lists('keyword','',$map,"*","id desc",$firstrow,$listrow,2);
    	if($count>$listrow){
    		$show['total'] = $count;
    		$show['listrow'] = $listrow;
    		$this->assign('show',$show);
    	}
    	$this->assign('list',$list);
    	$this->display();
    }
    /**
     * 搜素列表
     */
    public function search_list(){
    	$listrow = (int)I('pageSize');
    	if(empty($listrow)){
    		$listrow = 15;
    	}
    	$p = I('pageCurrent/d')?I('pageCurrent/d'):'1';
    	$count = M('search')->count();
    	$firstrow = ($p-1)*$listrow;
    	$map = 1;
    	$name = I('name');
    	if(!empty($name)){
    		$map .= " and name like '%".$name."%'";
    	}
    	$list = lists('search','',$map,"*","id desc",$firstrow,$listrow,2);
    	if($count>$listrow){
    		$show['total'] = $count;
    		$show['listrow'] = $listrow;
    		$this->assign('show',$show);
    	}
    	$this->assign('list',$list);
    	$this->display();
    }
    /**
     * 商品评论
     */
    public function comment(){
    	$listrow = (int)I('pageSize');
    	if(empty($listrow)){
    		$listrow = 15;
    	}
    	$p = I('pageCurrent/d')?I('pageCurrent/d'):'1';
    	
    	$firstrow = ($p-1)*$listrow;
    	$name = I('name');
    	if(!empty($name)){
    		$where['u.name'] = array('like',"%{$name}%");
    		$where['u.nickname'] = array('like',"%{$name}%");
    		$where['u.mobile'] = array('like',"%{$name}%");
    		$where['_logic'] = 'or';
    		$map['_complex'] = $where;
    	}
    	$content = I('content');
    	if($content){
    		$map['d.contents'] = array('like',"%{$content}%");
    	}
    	$map['d.parent_id'] = 0;
    	$join = "left join __USER__ as u on u.id = d.user_id  left join __GOODS__ as g on g.id = d.goods_id";
    	$count = M('discussion as d')->join($join)->count();
    	$list = lists('discussion as d',$join,$map,"d.*,u.mobile,g.name","d.id desc",$firstrow,$listrow,2);
    	if(!empty($list)){
    		foreach ($list as $i=>$va){
    			$list[$i]['replay'] = M('discussion')->where("parent_id='{$va['id']}'")->getField('contents');
    			if(!empty($va['photo'])){
    				$list[$i]['photo'] = explode(",", $va['photo']);
    			}
    			
     		}
    	}
    	if($count>$listrow){
    		$show['total'] = $count;
    		$show['listrow'] = $listrow;
    		$this->assign('show',$show);
    	}
    	$this->assign('list',$list);
    	$this->display();
    }
}


